<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */
class Templete{
    
    public static $theme_name = "Default";
    private $theme;
    public static $headerTheme;
    public static $footerTheme;
    
    public function loadThemeContent(){
        
        $this->theme = file_get_contents(VeCms::getPathTemplete()."/".self::$theme_name.".php");
        
        if ($this->theme){
            
            //cargando las etiquetas predeterminadas del sistema
            
            if (stripos($this->theme,"{title}")){
                $this->theme = str_replace("{title}", VeCms::$app->app_info['title'], $this->theme);
            }
            
            //cargando el contenido necesario para el funcionamiento del sistema.
            
            if (stripos($this->theme,"{javascript_base}") !== FALSE){
                $this->theme = str_replace("{javascript_base}", VeCms::loadJsBase(), $this->theme);
            }
            else{
                throw new Exception("No se encontro la etiqueta javascript base ");
            }
            
            if (stripos($this->theme,"{css_base}") !== FALSE){
                $this->theme = str_replace("{css_base}", VeCms::loadCssBase(), $this->theme);
            }
            else{
                throw new Exception("No se encontro la etiqueta css_base");
            }
            
            if (stripos($this->theme,"{javascript}") !== FALSE){
                $this->theme = str_replace("{javascript}", VeCms::getPathJs(), $this->theme);
            }
            
            if (stripos($this->theme,"{css_link}") !== FALSE){
                $this->theme = str_replace("{css_link}", VeCms::getPathCss(), $this->theme);
            }
            
            if (stripos($this->theme,"{pathimage}") !== FALSE){
                $this->theme = str_replace("{pathimage}", VeCms::getPathImage(), $this->theme);
            }
            
            
            //cargando el contendido del sitio;
            if (stripos($this->theme,"{content}") !== FALSE){
                $this->theme = explode("{content}",  $this->theme);
                self::$headerTheme = $this->theme[0];
                self::$footerTheme = $this->theme[1];
                
            }
            else{
                throw new Exception("No se encontro la etiqueta content en el templete");
            }
            
            // cargando las etiquetas personalizadas
            
            if (isset(VeCms::$app->includes) AND count(VeCms::$app->includes) > 0){
                foreach (VeCms::$app->includes as $etiqueta =>$value){
                    if (file_exists(PATH_ROOT.DIRECTORY_SEPARATOR."base".DIRECTORY_SEPARATOR."file".DIRECTORY_SEPARATOR.$value.".php")){
                        if (stripos(self::$headerTheme,$etiqueta) !== false){
                            $data = file_get_contents(PATH_ROOT.DIRECTORY_SEPARATOR."base".DIRECTORY_SEPARATOR."file".DIRECTORY_SEPARATOR.$value.".php");
                            self::$headerTheme = str_replace($etiqueta, $data, self::$headerTheme);
                        }
                        elseif (stripos(self::$footerTheme,$etiqueta) !== false){
                            $data = file_get_contents(PATH_ROOT.DIRECTORY_SEPARATOR."base".DIRECTORY_SEPARATOR."file".DIRECTORY_SEPARATOR.$value.".php");
                            self::$footerTheme = str_replace($etiqueta, $data, self::$footerTheme);
                        }
                    }
                    else{
                        if (stripos(self::$headerTheme,$etiqueta) !== false){
                            self::$headerTheme = str_replace($etiqueta, $value, self::$headerTheme);
                        }
                        elseif (stripos(self::$footerTheme,$etiqueta) !== false){
                            self::$footerTheme = str_replace($etiqueta, $value, self::$footerTheme);
                        }
                    }
                }
            }
            
        }
    }
}
?>
